# 主程序：

import json
import pickle
import os
from downloaders.downloader_PR_SC import Downloader_PR_SC
from analysiser.page_analyzer import PageAnalyzer

# 测试代码：
# 加载页面解析脚本：
script_filename = os.path.join(os.path.dirname(__file__), '.',
                               'page_analy_script', 'script_PR_SC.json')
script = ""
try:
    with open(script_filename, 'r', encoding='utf-8') as file:
        script = json.load(file)
except FileNotFoundError:
    print("文件未找到")
except json.JSONDecodeError:
    print("JSON 文件解析错误")
analyzer = PageAnalyzer()
analyzer.load_json_script(script)

dl = Downloader_PR_SC()
dl.set_limit_time("2022-01-01 00:00:00", "2022-12-31 23:59:59")
dl.set_keyword("遥感 中标公告")
doing = dl.try_get_datas()
print("records:", dl.get_records())
count = 0
while doing:
    datas = []
    datas = dl.get_datas()
    for d in datas:
        count += 1
        # print('解析后的数据:')
        # j_str = json.dumps(d,
        #                    default=lambda obj: obj.__dict__,
        #                    sort_keys=True,
        #                    indent=2,
        #                    ensure_ascii=False)
        # print(j_str)
        # print(d.tostring())
        # bytestr = pickle.dumps(d).encode('unicode_escape')
        # print(bytestr.decode('unicode_escape'))
        this_url = dl.build_inner_link_url(d.linkurl)
        print(count, ',', this_url, ',', d.publishTime, ',', d.createTime)

        # 解析页面：
        analyzer.load_page(this_url)
        analyzer.load_json_script(script)
        result = analyzer.analyze()
        print(result)
        print("-----------------------------------------------------------")

    doing = dl.to_next_page()
